package me.keiwu.pub.util.cos;

import com.alibaba.fastjson.JSON;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.request.DelFileRequest;
import com.qcloud.cos.request.UploadFileRequest;
import com.qcloud.cos.sign.Credentials;
import me.keiwu.pub.data.cos.CosResponse;
import me.keiwu.pub.data.cos.UploadResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * Created by kei on 07/12/2017.
 */
@Component
public class CosUtil {

    private static Logger logger = LoggerFactory.getLogger(CosUtil.class);

//    appid: 1255348930
//    SecretId AKIDo183mJ2ru8FF3kbbljaGoAPTL5dzIMaf
//    SecretKey 62mKgtwnRt8FAtQr8C4kEWL1Xrz52nLh

    @Value("${cos.appId}")
    private Long appId;
    @Value("${cos.secretId}")
    private String secretId;
    @Value("${cos.secretKey}")
    private String secretKey;


    private COSClient cosClient;



    @PostConstruct
    public void init() {
        if (this.appId == null || this.secretId == null || this.secretKey == null) {
            logger.warn("cos not init! need to add config for cos!");
            return;
        }
        Credentials cred = new Credentials(this.appId, this.secretId, this.secretKey);
        ClientConfig clientConfig = new ClientConfig();
        // 设置bucket所在的区域，比如华南园区：gz； 华北园区：tj；华东园区：sh ；
        clientConfig.setRegion("gz");
        this.cosClient = new COSClient(clientConfig, cred);
    }


    /**
     * 上传文件
     * @param bucketName
     * @param cosPath
     * @param localPath
     * @return
     */
    public UploadResponse upload(String bucketName, String cosPath, String localPath) {
        UploadFileRequest request = new UploadFileRequest(bucketName, cosPath, localPath);
        return this.doUpload(request);
    }
    /**
     * 上传图片
     * @param bucketName
     * @param cosPath
     * @param contentBuffer
     * @return
     */
    public UploadResponse upload(String bucketName, String cosPath, byte[] contentBuffer) {
        UploadFileRequest request = new UploadFileRequest(bucketName, cosPath, contentBuffer);
        return this.doUpload(request);
    }
    private UploadResponse doUpload(UploadFileRequest uploadFileRequest) {
        CosResponse resp = CosResponse.parse(cosClient.uploadFile(uploadFileRequest));
        if (resp.getCode() == 0) {
            return JSON.parseObject(resp.getData(), UploadResponse.class);
        }
        return null;
    }


    /**
     * 删除图片
     * @param bucketName
     * @param cosPath
     * @return
     */
    public CosResponse delete(String bucketName, String cosPath) {
        DelFileRequest delFileRequest = new DelFileRequest(bucketName, cosPath);
        return CosResponse.parse(cosClient.delFile(delFileRequest));
    }


    public static void main(String[] args) {
        CosUtil cosUtil = new CosUtil();
        cosUtil.appId = 1255411846L;
        cosUtil.secretId = "AKIDELI3kKdQlw1xltKlKpERTJxUuCJFBjDc";
        cosUtil.secretKey = "UoxviY6YrzwwlOfpobDdvCNwOfm7HZjm";
        cosUtil.init();

        String bucketName = "infinitus";
        String cosPath = "/gallery/test2.png";
        String localPath = "/Users/kei/Desktop/tmp.png";
//        UploadResponse resp = cosUtil.upload(bucketName, cosPath, localPath);
        CosResponse resp = cosUtil.delete(bucketName, cosPath);
        System.err.println(resp);
    }

    public static UploadResponse uploadTmp(byte[] content) {
        CosUtil cosUtil = new CosUtil();
        cosUtil.appId = 1255411846L;
        cosUtil.secretId = "AKIDELI3kKdQlw1xltKlKpERTJxUuCJFBjDc";
        cosUtil.secretKey = "UoxviY6YrzwwlOfpobDdvCNwOfm7HZjm";
        cosUtil.init();

        String bucketName = "infinitus";
        String cosPath = "/gallery/test2.png";
        UploadResponse resp = cosUtil.upload(bucketName, cosPath, content);
//        CosResponse resp = cosUtil.delete(bucketName, cosPath);
        System.err.println(resp);
        return resp;
    }



}
